<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h1 class="login-title">用户登录</h1>
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            required
            class="form-input"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
            required
            class="form-input"
          />
        </div>
        
        <div class="form-options">
          <div class="remember-me">
            <input type="checkbox" id="remember" v-model="rememberMe" />
            <label for="remember">记住我</label>
          </div>
          <router-link to="#" class="forgot-password">忘记密码?</router-link>
        </div>
        
        <button type="submit" class="login-button" :disabled="isLoading">
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
        
        <div class="register-link">
          <span>还没有账号?</span>
          <router-link to="/register" class="link-primary">立即注册</router-link>
        </div>
      </form>
      
      <!-- 错误提示 -->
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'

const router = useRouter()
const userStore = useUserStore()

const loginForm = ref({
  username: '',
  password: ''
})

const rememberMe = ref(false)
const isLoading = ref(false)
const errorMessage = ref('')

const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  isLoading.value = true
  errorMessage.value = ''
  
  try {
    const result = await userStore.login(loginForm.value)
    
    if (result.success) {
      // 登录成功后跳转到个人中心
      router.push('/user/profile')
    } else {
      errorMessage.value = '登录失败，请检查用户名和密码'
    }
  } catch (error) {
    console.error('登录错误:', error)
    errorMessage.value = '登录失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  padding: 20px;
  width: 100%;
}

.login-form-wrapper {
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 50px;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
}

.login-title {
  text-align: center;
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 40px;
  color: #333;
}

.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: block;
  margin-bottom: 10px;
  font-weight: 500;
  color: #555;
  font-size: 16px;
}

.form-input {
  width: 100%;
  padding: 16px 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 18px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #4285f4;
  box-shadow: 0 0 0 2px rgba(66, 133, 244, 0.1);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
}

.remember-me label {
  margin: 0;
  font-size: 16px;
  color: #666;
  cursor: pointer;
}

.forgot-password {
  font-size: 16px;
  color: #4285f4;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #3367d6;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 18px;
  background-color: #4285f4;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.login-button:hover:not(:disabled) {
  background-color: #3367d6;
}

.login-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  margin-top: 28px;
  font-size: 16px;
  color: #666;
}

.link-primary {
  color: #4285f4;
  text-decoration: none;
  margin-left: 4px;
  font-weight: 600;
  font-size: 16px;
}

.link-primary:hover {
  text-decoration: underline;
}

.error-message {
  margin-top: 20px;
  padding: 16px;
  background-color: #fee;
  color: #d32f2f;
  border-radius: 6px;
  font-size: 16px;
  text-align: center;
}

@media (max-width: 768px) {
  .login-form-wrapper {
    padding: 40px 25px;
    max-width: 90%;
  }
  
  .login-title {
    font-size: 28px;
    margin-bottom: 30px;
  }
  
  .form-input {
    padding: 14px 18px;
    font-size: 16px;
  }
  
  .login-button {
    padding: 16px;
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .login-form-wrapper {
    padding: 30px 20px;
    max-width: 100%;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .form-input {
    padding: 12px 16px;
    font-size: 15px;
  }
  
  .login-button {
    padding: 14px;
    font-size: 15px;
  }
  
  .form-group label,
  .remember-me label,
  .forgot-password,
  .register-link,
  .link-primary {
    font-size: 14px;
  }
}
</style>